package com.demo.controller;

import com.demo.common.Result;
import com.demo.service.PictureService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: lixiaodong
 * @Description:  图片上传功能
 * @Date: 2022/9/15 15:12
 * @Parms:
 * @ReturnType:
 */

@Slf4j
@RestController
@Api(tags = "图片上传功能")
public class PictureController extends BaseController{

    @Autowired
    PictureService pictureService;

    @PostMapping("/newsImage")
    public Result newsImage(@RequestParam("file") MultipartFile file, String name) {
      return pictureService.newsImage(file, name);
    }

    @RequestMapping("/delImage")
    public Result delImage(@RequestParam("fileNmae") String fileNmae){
      return   pictureService.delImage(fileNmae);
    }

    @RequestMapping("/updateImg")
    public Result updateImg(@RequestParam("file") MultipartFile file, String name,String oldName){
         if (pictureService.selectOne(oldName) != null) {
            this.delImage(oldName);
            this.newsImage(file,name);
        }
         return  Result.ok("修改成功");
    }
}
